package com.srwl.mytx.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.viewpager.widget.ViewPager;

import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.ShowBigImageViewPagerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import qiu.niorgai.StatusBarCompat;

import static com.yalantis.ucrop.UCropFragment.TAG;


/**
 * 全屏大图展示
 */
public class ShowBigImage extends FrameLayout {
    private static final String TAG = "ShowBigImage";
    private String myId;
    private Context context;
    protected ViewPager bigImageViewPager;
    protected ShowBigImageViewPagerAdapter showBigImageViewPagerAdapter;
    private ImageView blackBackGround;
    //View fl_bigPicture;
    AlphaAnimation in = new AlphaAnimation(0, 1);
    AlphaAnimation out = new AlphaAnimation(1, 0);
    private List<Message> imageMessageList;
    private Map<Integer, Info> infoMap = new HashMap<>();
    private boolean statusOfShowBigImage = false;
    private OnPageChangeListener onPageChangeListener;

    public ShowBigImage(Context context) {
        super(context);
        init(context);
    }

    public ShowBigImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShowBigImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
       // LayoutInflater.from(context).inflate(R.layout.frame_layout_big_image, this);
       // bigImageViewPager = findViewById(R.id.pager_big_picture);
       // blackBackGround = findViewById(R.id.img_bg_black);

    }

    public void initDate(List<Message> imageMessageList, String myId, OnPageChangeListener onPageChangeListener) {
        this.imageMessageList = imageMessageList;
        this.myId = myId;
        this.onPageChangeListener = onPageChangeListener;
        initViewPager();
    }

    public void setCurrentSmallImageRectInfo(int position, Info info) {
        infoMap.put(position, info);
        showBigImageViewPagerAdapter.setInfoMap(position, info);
    }

    private void initViewPager() {

        showBigImageViewPagerAdapter = new ShowBigImageViewPagerAdapter(imageMessageList, context, initHideBigImageListener(), myId);
        bigImageViewPager.setAdapter(showBigImageViewPagerAdapter);
        bigImageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //当大图页面左右滑动的时候，应该通知到 聊天列表，是为了当大图隐藏的时候，能从聊天列表中拿到小图的位置信息，做缩放动画，
                // 因为没法在缩放之前去拿对应的消息rect 信息，小图可能没有显示到可见位置，也不能立即滑动然后拿位置，因为滑动是需要时间的，
                // 所以只有聊天列表和大图同步滑动
                //做了100毫秒的延迟，是为了当 在聊天页面中点击的item 需要移动到完全可见的位置，让这个移动过程不可见，
                Log.d(TAG, "onPageSelected:页面切换了 position ：" + position);
                if (onPageChangeListener != null) {
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            onPageChangeListener.onPageChange(position);
                        }
                    }, 100);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //大图黑色背景显示隐藏 持续时间
        in.setDuration(500);
        in.setFillAfter(true);
        out.setDuration(400);
        out.setFillAfter(true);
        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                blackBackGround.setVisibility(View.INVISIBLE);
                ShowBigImage.this.setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public void getSmallImageInfo(int position) {
        if (onPageChangeListener != null) {
            Map<Integer, Info> currentInfoMap = onPageChangeListener.onGetImageRectInfo(position);
            infoMap.putAll(currentInfoMap);
            showBigImageViewPagerAdapter.setInfoMap(currentInfoMap);
        }
    }

    private ShowBigImageViewPagerAdapter.OnHideBigImageListener initHideBigImageListener() {

        return new ShowBigImageViewPagerAdapter.OnHideBigImageListener() {
            @Override
            public boolean beforeHideBigImage(int position) {
                //  ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //如果展示大图的是全屏，则隐藏大图的时候， 需要去掉全屏效果
                getSmallImageInfo(position);
                hideBigImage();
                return false;
            }

            @Override
            public void showAnima() {
                bigImageViewPager.setVisibility(GONE);
            }
        };
    }

    public void showBigImage() {
        setStatusOfShowBigImage(true);
        ShowBigImage.this.setVisibility(VISIBLE);
        blackBackGround.setVisibility(View.VISIBLE);
        postDelayed(() -> blackBackGround.startAnimation(in), 100);
    }

    public void hideBigImage() {
        setStatusOfShowBigImage(false);
        blackBackGround.startAnimation(out);
    }

    public boolean isStatusOfShowBigImage() {
        return statusOfShowBigImage;
    }

    public void setStatusOfShowBigImage(boolean statusOfShowBigImage) {
        this.statusOfShowBigImage = statusOfShowBigImage;
    }

    public void refreshBigImage() {
        showBigImageViewPagerAdapter.notifyDataSetChanged();
    }

    public void seekAndRefresh(int position) {


        refreshBigImage();
        bigImageViewPager.setCurrentItem(position, false);


    }

    public boolean onBackPressed() {
        if (statusOfShowBigImage) {

            int currentItem = bigImageViewPager.getCurrentItem();
            getSmallImageInfo(currentItem);
            // View child = bigImageViewPager.getChildAt(currentItem);
          //  View itemView = (View) showBigImageViewPagerAdapter.instantiateItem(null, currentItem);
            hideBigImage();
            //PhotoView photoView = itemView.findViewById(R.id.iv_goods_picture);
            //showBigImageViewPagerAdapter.bigImageAnimateOut(currentItem, photoView);
            return false;
        } else {
            return true;
        }
    }

    public interface OnPageChangeListener {
        void onPageChange(int position);

        Map<Integer, Info> onGetImageRectInfo(int position);
    }
}
